package com.example.my_objectives.model;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author mohamed
 *
 */
public class Recurrence extends Database {

	private final String	LOG	= Recurrence.class.getName();

	private long			_id;
	private String			_name;
	private long			_years;
	private long			_months;
	private long			_days;
	private long			_hours;
	private long			_minutes;
	private long			_seconds;

	public Recurrence(SQLiteDatabase db) {
		super(db);
		set_database(db);
	}

	public Recurrence(SQLiteDatabase db, long id, String name, long years, long months, long days, long hours,
			long minutes, long seconds) {
		super(db);
		set_database(db);
		this._id = id;
		this._name = name;
		this._years = years;
		this._months = months;
		this._days = days;
		this._hours = hours;
		this._minutes = minutes;
		this._seconds = seconds;
	}

	public long getId() {
		return _id;
	}

	public void setId(long id) {
		this._id = id;
	}

	public String getName() {
		return _name;
	}

	public void setName(String name) {
		this._name = name;
	}

	public long getYears() {
		return _years;
	}

	public void setYears(long years) {
		this._years = years;
	}

	public long getMonths() {
		return _months;
	}

	public void setMonths(long months) {
		this._months = months;
	}

	public long getDays() {
		return _days;
	}

	public void setDays(long days) {
		this._days = days;
	}

	public long getHours() {
		return _hours;
	}

	public void setHours(long hours) {
		this._hours = hours;
	}

	public long getMinutes() {
		return _minutes;
	}

	public void setMinutes(long minutes) {
		this._minutes = minutes;
	}

	public long getSeconds() {
		return _seconds;
	}

	public void setSeconds(long seconds) {
		this._seconds = seconds;
	}

	@Override
	public Exception createTable() {
		Exception returnValue = null;
		try {
			String query = "CREATE TABLE "
				+ TableName.recurrence.toString()
				+ "(id INTEGER, name TEXT, years INTEGER, months INTEGER, days INTEGER, hours INTEGER, minutes INTEGER, seconds INTEGER, "
				+ "CONSTRAINT 'recurrence_pk' PRIMARY KEY(id))";
			Log.d(LOG, query);
			get_database().execSQL(query);
		}// try
		catch (Exception e) {
			returnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return returnValue;
	}

	@Override
	public Exception add() {
		Exception eReturnValue = null;
		try {
			ContentValues values = new ContentValues();
			if (_id != -1) {
				values.put("id", _id);
			}//if
			values.put("name", _name);
			values.put("years", _years);
			values.put("months", _months);
			values.put("days", _days);
			values.put("hours", _hours);
			values.put("minutes", _minutes);
			values.put("seconds", _seconds);

			get_database().insert(TableName.recurrence.toString(), null, values);
		}// try
		catch (Exception e) {
			eReturnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return eReturnValue;
	}

	@Override
	public Exception update(long old_id) {
		Exception eReturnValue = null;
		try {
			ContentValues values = new ContentValues();
			values.put("id", _id);
			values.put("name", _name);
			values.put("years", _years);
			values.put("months", _months);
			values.put("days", _days);
			values.put("hours", _hours);
			values.put("minutes", _minutes);
			values.put("seconds", _seconds);

			get_database().update(TableName.recurrence.toString(), values, "id = ?",
					new String[] { Long.toString(old_id) });
		}// try
		catch (Exception e) {
			eReturnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return eReturnValue;
	}

	@Override
	public Exception delete() {
		Exception eReturnValue = null;
		try {
			String query = "DELETE FROM " + TableName.recurrence.toString() + " WHERE id = " + _id;
			Log.d(LOG, query);
			get_database().execSQL(query);
		}// try
		catch (Exception e) {
			eReturnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return eReturnValue;
	}

	public Exception defaultInserts() {
		Exception returnValue = null;
		try {
			Log.d(LOG, "*** Table inserts ***");
			String query = "INSERT INTO " + TableName.recurrence.toString()
				+ " (`id`, `name`, `years`, `months`, `days`, `hours`, `minutes`, `seconds`) "
				+ "VALUES (1, 'Yearly', 1, 0, 0, 0, 0, 0)";
			Log.d(LOG, query);
			get_database().execSQL(query);

			query = "INSERT INTO " + TableName.recurrence.toString()
				+ " (`id`, `name`, `years`, `months`, `days`, `hours`, `minutes`, `seconds`) "
				+ "VALUES (2, 'Monthly', 0, 1, 0, 0, 0, 0)";
			Log.d(LOG, query);
			get_database().execSQL(query);

			query = "INSERT INTO " + TableName.recurrence.toString()
				+ " (`id`, `name`, `years`, `months`, `days`, `hours`, `minutes`, `seconds`) "
				+ "VALUES (3, 'Weekly', 0, 0, 7, 0, 0, 0)";
			Log.d(LOG, query);
			get_database().execSQL(query);

			query = "INSERT INTO " + TableName.recurrence.toString()
				+ " (`id`, `name`, `years`, `months`, `days`, `hours`, `minutes`, `seconds`) "
				+ "VALUES (4, 'Daily', 0, 0, 1, 0, 0, 0)";
			Log.d(LOG, query);
			get_database().execSQL(query);

			query = "INSERT INTO " + TableName.recurrence.toString()
				+ " (`id`, `name`, `years`, `months`, `days`, `hours`, `minutes`, `seconds`) "
				+ "VALUES (5, 'Hourly', 0, 0, 0, 1, 0, 0)";
			Log.d(LOG, query);
			get_database().execSQL(query);

		}// try
		catch (Exception e) {
			returnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return returnValue;
	}

	/**
	 * All recurrences
	 *
	 * @return All records in the table
	 */
	public ArrayList<ContentValues> getAll() {

		ArrayList<ContentValues> list = new ArrayList<ContentValues>();

		try {
			String query = "SELECT * FROM " + TableName.recurrence.toString();
			Log.d(LOG, query);
			Cursor cursor = get_database().rawQuery(query, null);
			if (cursor.moveToFirst()) {
				while (!cursor.isAfterLast()) {
					ContentValues record = new ContentValues();
					record.put("id", cursor.getString(cursor.getColumnIndex("id")));
					record.put("name", cursor.getString(cursor.getColumnIndex("name")));
					record.put("years", cursor.getString(cursor.getColumnIndex("years")));
					record.put("months", cursor.getString(cursor.getColumnIndex("months")));
					record.put("days", cursor.getString(cursor.getColumnIndex("days")));
					record.put("hours", cursor.getString(cursor.getColumnIndex("hours")));
					record.put("minutes", cursor.getString(cursor.getColumnIndex("minutes")));
					record.put("seconds", cursor.getString(cursor.getColumnIndex("seconds")));

					list.add(record);
				}//while
			}// if
			cursor.close();
		}//try
		catch (Exception e) {
			Log.e(LOG, e.getMessage());
		}//catch
		return list;
	}

	@Override
	public Exception load(long id) {
		Exception returnValue = null;
		try {
			String query = "SELECT * FROM " + TableName.recurrence.toString() + " WHERE id = " + id;
			Log.d(LOG, query);
			Cursor cursor = get_database().rawQuery(query, null);

			if (cursor.moveToFirst()) {
				_id = id;
				_name = cursor.getString(cursor.getColumnIndex("name"));
				_years = cursor.getInt(cursor.getColumnIndex("years"));
				_months = cursor.getInt(cursor.getColumnIndex("months"));
				_days = cursor.getInt(cursor.getColumnIndex("days"));
				_hours = cursor.getInt(cursor.getColumnIndex("hours"));
				_minutes = cursor.getInt(cursor.getColumnIndex("minutes"));
				_seconds = cursor.getInt(cursor.getColumnIndex("seconds"));
			}// if
			cursor.close();
		}// try
		catch (Exception e) {
			returnValue = e;
			Log.e(LOG, e.getMessage());
		}// catch
		return returnValue;
	}

}
